package cn.handong.tool.utils;

import org.junit.Test;
import org.springframework.util.StopWatch;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class 时间相关 {
    public static void main(String[] args) {
        // java8推出的全新时间类
        System.out.println(new Timestamp(LocalDateTime
                .now()
                .minusHours(24)
                .toInstant(ZoneOffset
                        .of("+8"))
                .toEpochMilli()));

        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd 'at' HH:mm:ss z");
        Date date = new Date(System.currentTimeMillis());
        System.out.println(formatter.format(date));

        SimpleDateFormat formatter1= new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss");
        Date date1 = new Date(System.currentTimeMillis());
        System.out.println(formatter1.format(date1));

        SimpleDateFormat formatter2= new SimpleDateFormat("yyyy-MM-dd");
        Date date2 = new Date(System.currentTimeMillis());
        System.out.println(formatter2.format(date2));

    }

    @Test
    public void test1(){
        /**
         * LocalDate 一般只显示日期，没有时间。这意味着我们只能获取当前日期，但没有一天的具体时间。
         *
         */
        LocalDate date = LocalDate.now(); // get the current date
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd-MM-yyyy");
        System.out.println(date.format(formatter));

        /**
         * LocalTime 则与 LocalDate 相反，它只能获取时间，而不能获取日期。
         */
        LocalTime time = LocalTime.now(); // get the current time
        DateTimeFormatter formatter1 = DateTimeFormatter.ofPattern("HH:mm:ss");
        System.out.println(time.format(formatter1));

        /**
         * LocalDateTime 顾名思义，就是将以上两者联系起来，即可以获取日期，又可以获取时间。
         * 因此， LocalDateTime 是Java中最常用的 Date/Time 类。
         */
        LocalDateTime dateTime = LocalDateTime.now(); // get the current date and time
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("dd-MM-yyyy HH:mm:ss");
        System.out.println(dateTime.format(formatter2));
    }

    /**
     * 统计执行时间
     * @throws InterruptedException
     */
    @Test
    public void test() throws InterruptedException {
        List<Integer> list = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            list.add(i);
        }

        int size = list.size();

        StopWatch myWatch = new StopWatch("myWatch");
        myWatch.start("test1");
//        Thread.sleep(1000L);
        for (Integer li : list) {
            System.out.println(li * li);
        }
        myWatch.stop();
        myWatch.start("test2");
//        Thread.sleep(2000L);
        list.forEach(li -> {
            System.out.println(li * li);
        });
        myWatch.stop();
        myWatch.start("test3");
//        Thread.sleep(8000L);

        for (int i = 0; i < size; i++) {
            Integer integer = list.get(i);
            System.out.println(integer * integer);
        }

        myWatch.stop();
        System.out.println(myWatch.prettyPrint());
    }
}